<?php
/**
 * Created by PhpStorm.
 * User: 50223
 * Date: 18.8.10
 * Time: 9:11
 */
namespace App\Http\Controllers\Dangerous;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Session;
use App\Models\Dangerous;
use Illuminate\Support\Facades\DB;
class DangerousController extends Controller
{
    public function dangerous(Request $request){
//        $Dangerous = new Dangerous();
//        $data = $Dangerous->dangerous_list('dangerous.id',0,20);


        $dangerousList =  DB::table('dangerous')
            ->join('dangerous_alias','dangerous.id','=','dangerous_alias.dangerous_id')
            ->select('dangerous.*','dangerous_alias.alias')
            ->groupBy('dangerous.id')
            ->orderBy('id','desc')->paginate(20);
        $historyAll = DB::table('history')->where('type','=',7)->paginate(20);

//        所有的产品

        return view('/dangerous/dangerous',[
            'dangerousList'=>$dangerousList,
            'historyAll'=>$historyAll

        ]);
    }

    public function dangerous_add(Request $request){

        if($request->isMethod('POST')){
//                    注意此userId为大写
            $userId = $request->session()->get('userId');

            if(!$userId) {

                return array('status' => 2, 'msg' => '登录过期，请重新登录');exit;
            }else{
                $goods = $_POST['Goods'];

                $upId = explode(',',$_GET['upId']);
                $downId = explode(',',$_GET['downId']);

                $good_name = DB::table('dangerous')->where('xl_name','=',$goods['xl_name'])->get();
                if($good_name->count() !=0){
                    return array('status' => 0, 'msg' => '该危化品已经存在，请勿重复增加');exit;
                }
                $alias = $goods['alias'];
                unset($goods['alias']);

                $res = DB::table('dangerous')->insertGetId($goods);
                if(count($upId)>0){
                    for ($i= 0;$i<count($upId);$i++){
                        DB::table('dangerous_upstream')->insert(['dangerous_id'=>$res,'goods_id'=>$upId[$i]]);
                    }
                }
                if(count($downId)>0){
                    for ($i= 0;$i<count($downId);$i++){
                        DB::table('dangerous_downstream')->insert(['dangerous_id'=>$res,'goods_id'=>$downId[$i]]);
                    }
                }

               $rs =  DB::table('dangerous_alias')->insert([
                    'alias'=>$alias,
                    'dangerous_id'=>$res
               ]);

                $username = DB::table('users')->where('id','=',$userId)->value('username');

                $history = DB::table('history')->insert([
                    'userid'=>$userId,
                    'username'=>$username,
                    'created_time'=>date('Y-m-d H:i:s',time()),
                    'description'=>'新增危化品',
                    'tag'=>'新增危化品',
                    'type'=>7
                ]);

                    if($history){
                        return array('status'=>1,'msg'=>'危化品新增成功');exit;
                    }else{
                        return array('status'=>0,'msg'=>'危化品新增失败');
                    }
                }
            }
            $goods = DB::table('tp_goods')->select('goods_id','goods_name','cas')->orderBy('goods_id','desc')->paginate(20);
            return view('/dangerous/dangerous_add',[
                'goods'=>$goods
            ]);




    }

    public function dangerous_details($id){

        $dangerousList =  DB::table('dangerous')
            ->join('dangerous_alias','dangerous.id','=','dangerous_alias.dangerous_id')
            ->select('dangerous.*','dangerous_alias.alias')
            ->groupBy('dangerous.id')
            ->where('dangerous.id','=',$id)
            ->first();
        $dangerous_upstream =   DB::table('dangerous_upstream')
            ->join('tp_goods','dangerous_upstream.goods_id','=','tp_goods.goods_id')
            ->select('tp_goods.goods_id','tp_goods.original_bn','tp_goods.goods_name','tp_goods.cas')
            ->where('dangerous_upstream.dangerous_id','=',$id)
            ->get();
        $dangerous_downstream = DB::table('dangerous_downstream')
            ->join('tp_goods','dangerous_downstream.goods_id','=','tp_goods.goods_id')
            ->select('tp_goods.goods_id','tp_goods.original_bn','tp_goods.goods_name','tp_goods.cas')
            ->where('dangerous_downstream.dangerous_id','=',$id)
            ->get();
        $historyAll = DB::table('history')->where('type','=',7)->paginate(20);

        return view('/dangerous/dangerous_details',[
            'dangerousList'=>$dangerousList,
            'historyAll'=>$historyAll,
            'dangerous_upstream'=>$dangerous_upstream,
            'dangerous_downstream'=>$dangerous_downstream,
        ]);
    }

    public function correction(Request $request,$id){


        if($request->isMethod('POST')){

            $userId = $request->session()->get('userId');
            $upId = explode(',',$_GET['upId']);
            $downId = explode(',',$_GET['downId']);

            if(!$userId) {

                return array('status' => 2, 'msg' => '登录过期，请重新登录');exit;
            }else{
                $goods = $_POST['Goods'];
                $alias = $goods['alias'];
                unset($goods['alias']);

                $res = DB::table('dangerous')->where('id','=',$id)->update($goods);

                $rs =  DB::table('dangerous_alias')->where('dangerous_id',$id)->update([
                    'alias'=>$alias,
                ]);


                $username = DB::table('users')->where('id','=',$userId)->value('username');

                $history = DB::table('history')->insert([
                    'userid'=>$userId,
                    'username'=>$username,
                    'created_time'=>date('Y-m-d H:i:s',time()),
                    'description'=>'危化品纠错',
                    'tag'=>'危化品纠错',
                    'type'=>7
                ]);
                if(count($upId)>0){
                    for ($i= 0;$i<count($upId);$i++){
                        DB::table('dangerous_upstream')->whereNotIn('goods_id',$upId)->insert(['dangerous_id'=>$id,'goods_id'=>$upId[$i]]);
                    }
                }
                if(count($downId)>0){
                    for ($i= 0;$i<count($downId);$i++){
                        DB::table('dangerous_downstream')->whereNotIn('goods_id',$downId)->insert(['dangerous_id'=>$id,'goods_id'=>$downId[$i]]);
                    }
                }

                if($history){
                    return array('status'=>1,'msg'=>'危化品纠错成功');exit;
                }else{
                    return array('status'=>0,'msg'=>'危化品纠错失败');
                }
            }
        }else{
            $dangerousList =  DB::table('dangerous')
                ->join('dangerous_alias','dangerous.id','=','dangerous_alias.dangerous_id')
                ->select('dangerous.*','dangerous_alias.alias')
                ->groupBy('dangerous.id')
                ->where('dangerous.id','=',$id)
                ->first();
//            获取当前产品的上游产品和下游产品
            $dangerous_upstream = DB::table('dangerous_upstream')
                ->join('tp_goods','dangerous_upstream.goods_id','=','tp_goods.goods_id')
                ->select('tp_goods.goods_id','tp_goods.original_bn','tp_goods.goods_name','tp_goods.cas')
                ->where('dangerous_upstream.dangerous_id','=',$id)
                ->get();

            $dangerous_downstream = DB::table('dangerous_downstream')
                ->join('tp_goods','dangerous_downstream.goods_id','=','tp_goods.goods_id')
                ->select('tp_goods.goods_id','tp_goods.original_bn','tp_goods.goods_name','tp_goods.cas')
                ->where('dangerous_downstream.dangerous_id','=',$id)
                ->get();
            $goods = DB::table('tp_goods')->select('goods_id','goods_name','cas')->orderBy('goods_id','desc')->paginate(20);

            return view('/dangerous/correction',[
                'dangerousList'=>$dangerousList,
                'dangerous_upstream'=>$dangerous_upstream,
                'dangerous_downstream'=>$dangerous_downstream,
                'goods'=>$goods
            ]);
        }

    }

//    筛选事件

        public function goods_id_screen(Request $request){

            if($request->isMethod('POST')){

                $cust_id = trim($_POST['cust_id']);

                $dataList = DB::table('dangerous')
                    ->join('dangerous_alias','dangerous.dangerous_id','=','dangerous_alias.dangerous_id')
                    ->select('dangerous.*','dangerous_alias.alias')
                    ->groupBy('dangerous.id')
                    ->where('dangerous.dangerous_id','like','%'.$cust_id.'%')
                    ->orderBy('id','desc')->paginate(20);


                if($dataList->count() !=0){
                    return array('status'=>1,'msg'=>'共查询到'.$dataList->count().'条结果','dataList'=>$dataList);
                }else{
                    return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据');

                }

            }

        }
    public function cust_xl_name_screen(Request $request){

        if($request->isMethod('POST')){

            $cust_xl_name = trim($_POST['cust_xl_name']);

            $dataList = DB::table('dangerous')
                ->join('dangerous_alias','dangerous.dangerous_id','=','dangerous_alias.dangerous_id')
                ->select('dangerous.*','dangerous_alias.alias')
                ->groupBy('dangerous.id')
                ->where('dangerous.xl_name','like','%'.$cust_xl_name.'%')
                ->orderBy('id','desc')->paginate(20);


            if($dataList->count() !=0){
                return array('status'=>1,'msg'=>'共查询到'.$dataList->count().'条结果','dataList'=>$dataList);
            }else{
                return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据');

            }

        }

    }

    public function cust_zh_name_screen(Request $request){

        if($request->isMethod('POST')){

            $cust_zh_name = trim($_POST['cust_zh_name']);

            $dataList = DB::table('dangerous')
                ->join('dangerous_alias','dangerous.dangerous_id','=','dangerous_alias.dangerous_id')
                ->select('dangerous.*','dangerous_alias.alias')
                ->groupBy('dangerous.id')
                ->where('dangerous.zh_name','like','%'.$cust_zh_name.'%')
                ->orderBy('id','desc')->paginate(20);


            if($dataList->count() !=0){
                return array('status'=>1,'msg'=>'共查询到'.$dataList->count().'条结果','dataList'=>$dataList);
            }else{
                return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据');

            }

        }

    }
    public function legal_name_fenzi_screen(Request $request){

        if($request->isMethod('POST')){

            $legal_name_fenzi = trim($_POST['legal_name_fenzi']);

            $dataList = DB::table('dangerous')
                ->join('dangerous_alias','dangerous.dangerous_id','=','dangerous_alias.dangerous_id')
                ->select('dangerous.*','dangerous_alias.alias')
                ->groupBy('dangerous.id')
                ->where('dangerous.molecule','like','%'.$legal_name_fenzi.'%')
                ->orderBy('id','desc')->paginate(20);


            if($dataList->count() !=0){
                return array('status'=>1,'msg'=>'共查询到'.$dataList->count().'条结果','dataList'=>$dataList);
            }else{
                return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据');

            }

        }

    }

    public function alias_screen(Request $request){

        if($request->isMethod('POST')){

            $alias = trim($_POST['alias']);

            $dataList = DB::table('dangerous')
                ->join('dangerous_alias','dangerous.dangerous_id','=','dangerous_alias.dangerous_id')
                ->select('dangerous.*','dangerous_alias.alias')
                ->groupBy('dangerous.id')
                ->where('dangerous_alias.alias','like','%'.$alias.'%')
                ->orderBy('id','desc')->paginate(20);


            if($dataList->count() !=0){
                return array('status'=>1,'msg'=>'共查询到'.$dataList->count().'条结果','dataList'=>$dataList);
            }else{
                return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据');

            }

        }

    }
    public function cas_screen(Request $request){

        if($request->isMethod('POST')){

            $cas = trim($_POST['cas']);

            $dataList = DB::table('dangerous')
                ->join('dangerous_alias','dangerous.dangerous_id','=','dangerous_alias.dangerous_id')
                ->select('dangerous.*','dangerous_alias.alias')
                ->groupBy('dangerous.id')
                ->where('dangerous.cas','like','%'.$cas.'%')
                ->orderBy('id','desc')->paginate(20);


            if($dataList->count() !=0){
                return array('status'=>1,'msg'=>'共查询到'.$dataList->count().'条结果','dataList'=>$dataList);
            }else{
                return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据');

            }

        }

    }

//   上游原料
    public function dangerous_upstream(Request $request){
        if($request->isMethod('POST')){

            $allid = $_POST['allId'];
//           执行两个操作 将选中的数据增加到html  中  在增加数据操作 
           for($i=0;$i<count($allid);$i++){

               $data[] = DB::table('tp_goods')->where('goods_id',$allid[$i])->first();
           }
           if($data){
//               将数据增加到上游产品表
               return array('status'=>1,'dataList'=>$data);
           }

        }

    }
    public function dangerous_downstream(Request $request){
        if($request->isMethod('POST')){

            $allid = $_POST['allId'];
//           执行两个操作 将选中的数据增加到html  中  在增加数据操作
            for($i=0;$i<count($allid);$i++){

                $data[] = DB::table('tp_goods')->where('goods_id',$allid[$i])->first();
            }
            if($data){
//               将数据增加到上游产品表
                return array('status'=>1,'dataList'=>$data);
            }

        }

    }


    public function location(Request $request){

        $page = $_GET['page'];

        if($page==1){
            $page=10;
        }else{
            $page  = $page*10;
        }
        $goods = DB::table('tp_goods')->select('goods_id','goods_name','cas')->orderBy('goods_id','desc')->offset($page)->limit(10)->get();
        $pages = DB::table('tp_goods')->select('goods_id','goods_name','cas')->orderBy('goods_id','desc')->get();
        $pages = $pages->count()/20;

        return array('data'=>$goods,'pages'=>$pages);
    }

    public function dangerous_up_delete(Request $request){
        if($request->isMethod('POST')){

            $allId = $_POST['allId'];
            for ($i=0;$i<count($allId);$i++){
                $res = DB::table('dangerous_upstream')->where('goods_id',$allId[$i])->delete();
            }
            if($res){
                return array('status'=>1,'msg'=>'删除成功');
            }else{
                return array('status'=>0,'msg'=>'删除失败');
            }
        }
    }

    public function dangerous_down_delete(Request $request){

        if($request->isMethod('POST')){

            $allId = $_POST['allId'];
            for ($i=0;$i<count($allId);$i++){
                $res = DB::table('dangerous_downstream')->where('goods_id',$allId[$i])->delete();
            }
            if($res){
                return array('status'=>1,'msg'=>'删除成功');
            }else{
                return array('status'=>0,'msg'=>'删除失败');
            }
        }
    }

}